package com.fr.report.script.core.parser;

import com.fr.base.core.DateUtils;
import com.fr.report.script.Calculator;
import com.fr.util.Utils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class AddExpression extends MathExpression
{
  private static final String PLUS = "+";
  private static final String MINUS = "-";
  String[] ops;
  MultiExpression[] multis;

  AddExpression(MultiExpression[] paramArrayOfMultiExpression, String[] paramArrayOfString)
  {
    this.multis = paramArrayOfMultiExpression;
    this.ops = paramArrayOfString;
  }

  public Object eval(Calculator paramCalculator)
    throws UtilEvalError
  {
    if (this.multis.length == 0)
      throw new InterpreterError("multiList should not be empty");
    Object localObject1 = null;
    for (int i = 0; i < this.multis.length; ++i)
      if (i == 0)
      {
        localObject1 = this.multis[i].eval(paramCalculator);
      }
      else
      {
        Object localObject2 = this.multis[i].eval(paramCalculator);
        localObject1 = binaryOperation(localObject1, localObject2, this.ops[(i - 1)]);
      }
    return localObject1;
  }

  private static boolean isPLUS(String paramString)
  {
    return "+".equals(paramString);
  }

  private static boolean isMINUS(String paramString)
  {
    return "-".equals(paramString);
  }

  protected Object ccNINI(String paramString)
  {
    return ((isPLUS(paramString)) ? OperationUtils.NEGATIVE_INFINITY : OperationUtils.ZERO);
  }

  protected Object ccNIPI(String paramString)
  {
    return ((isPLUS(paramString)) ? OperationUtils.ZERO : OperationUtils.NEGATIVE_INFINITY);
  }

  protected Object ccNIRE(Object paramObject, String paramString)
  {
    return OperationUtils.NEGATIVE_INFINITY;
  }

  protected Object ccPINI(String paramString)
  {
    return ((isPLUS(paramString)) ? OperationUtils.ZERO : OperationUtils.POSITIVE_INFINITY);
  }

  protected Object ccPIPI(String paramString)
  {
    return ((isPLUS(paramString)) ? OperationUtils.POSITIVE_INFINITY : OperationUtils.ZERO);
  }

  protected Object ccPIRE(Object paramObject, String paramString)
  {
    return OperationUtils.POSITIVE_INFINITY;
  }

  protected Object ccRENI(Object paramObject, String paramString)
  {
    return ((isPLUS(paramString)) ? OperationUtils.NEGATIVE_INFINITY : OperationUtils.POSITIVE_INFINITY);
  }

  protected Object ccREPI(Object paramObject, String paramString)
  {
    return ((isPLUS(paramString)) ? OperationUtils.POSITIVE_INFINITY : OperationUtils.NEGATIVE_INFINITY);
  }

  protected Object ccRERE(Object paramObject1, Object paramObject2, String paramString)
    throws UtilEvalError
  {
    if ((paramObject1 instanceof Date) && (paramObject2 instanceof Number))
    {
      int i = ((Number)paramObject2).intValue();
      return DateUtils.datePlusInteger((Date)paramObject1, (isPLUS(paramString)) ? i : -i);
    }
    if ((((paramObject1 instanceof String) || (paramObject2 instanceof String))) && (isPLUS(paramString)))
      return Utils.objectToString(paramObject1) + Utils.objectToString(paramObject2);
    if ((paramObject1 instanceof Date) && (paramObject2 instanceof Date) && (isMINUS(paramString)))
      return new Integer((int)DateUtils.subtractDate((Date)paramObject1, (Date)paramObject2, "d"));
    return numberOperation(paramObject1, paramObject2, paramString);
  }

  protected Object decimalOperation(BigDecimal paramBigDecimal1, BigDecimal paramBigDecimal2, String paramString)
  {
    if (isPLUS(paramString))
      return paramBigDecimal1.add(paramBigDecimal2);
    return paramBigDecimal1.subtract(paramBigDecimal2);
  }

  protected Object doubleOperation(double paramDouble1, double paramDouble2, String paramString)
  {
    if (isPLUS(paramString))
      return new Double(paramDouble1 + paramDouble2);
    return new Double(paramDouble1 - paramDouble2);
  }

  protected Object intOperation(int paramInt1, int paramInt2, String paramString)
  {
    if (isPLUS(paramString))
      return new Integer(paramInt1 + paramInt2);
    return new Integer(paramInt1 - paramInt2);
  }

  public void traversal4Tiny(TinyHunter paramTinyHunter)
  {
    for (int i = 0; i < this.multis.length; ++i)
      this.multis[i].traversal4Tiny(paramTinyHunter);
  }

  public String exString(Calculator paramCalculator)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.multis.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(' ').append(this.ops[(i - 1)]).append(' ');
      localStringBuffer.append(this.multis[i].exString(paramCalculator));
    }
    return localStringBuffer.toString();
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.multis.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(' ').append(this.ops[(i - 1)]).append(' ');
      localStringBuffer.append(this.multis[i]);
    }
    return localStringBuffer.toString();
  }

  public String getExpression(int paramInt1, int paramInt2, int paramInt3, int paramInt4, boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.multis.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(' ').append(this.ops[(i - 1)]).append(' ');
      localStringBuffer.append(this.multis[i].getExpression(paramInt1, paramInt2, paramInt3, paramInt4, paramBoolean));
    }
    return localStringBuffer.toString();
  }

  public String[] parserParameter()
  {
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j = this.multis.length;
    while (i < j)
    {
      localArrayList.addAll(Arrays.asList(this.multis[i].parserParameter()));
      ++i;
    }
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }
}